Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] P3385 #1

Open
wants to merge 28 commits into
base: p2996
Choose a base branch
from
Open

[WIP] P3385 #1

wants to merge 28 commits into from

Conversation

zebullax
Copy link
Owner

@zebullax zebullax commented Oct 6, 2024

Proposal

Disclaimer

⚠️ This is a hacky experimentation to survey implementation feasibility...
It goes down happy paths so YMMV, 🟢 stands for This work in a controlled environment, probably break in a lot of cases

Status

Feature Status Comment
^^[[nodiscard]]; 🟢
std::meta::has_identifier(r) 🟢
std::meta::is_attribute(r) 🟢
std::meta::identifier_of(r) 🟢
std::meta::attributes_of(r) 🟢
[[ [: r :] ]] 🟠 Supports r reflecting a std attribute, a type
data_member_options_t{ .attributes { ... 🔴 Descoped away from 3385

Test

Throwaway smoke test ( really it will mostly not compile if tests don't pass...)

#include <experimental/meta>
#include <string>
#include <iostream>
#include <utility>

struct Witness {
  int nodiscard;
  int maybe_unused;
  int deprecated;
};

struct [[nodiscard, maybe_unused]] Foo;

consteval auto member_named(std::string_view name) {
  for (std::meta::info field : nonstatic_data_members_of(^^Witness)) {
    if (has_identifier(field) && identifier_of(field) == name)
      return field;
  }
  std::unreachable();
}

consteval bool testIsAttribute() {
  constexpr auto attr = ^^[[nodiscard]];
  return std::meta::is_attribute(attr);
}

consteval bool testIdentifierOf() {
  constexpr auto attr = ^^[[deprecated]];
  Witness s{0, 0, 0};
  s.[: member_named(std::meta::identifier_of(attr)) :]++;
  return s.deprecated == 1;
}

consteval bool testAttributesOfAttr() {
  constexpr auto attr = ^^[[nodiscard]];

  return std::meta::identifier_of(std::meta::attributes_of(attr)[0]) == "nodiscard"
    && std::meta::attributes_of(attr).size() == 1;
}

consteval bool testAttributesOfType() {
    Witness s{0, 0, 0};
    s.[: member_named(std::meta::identifier_of(std::meta::attributes_of(^^Foo)[0])) :]++;
    s.[: member_named(std::meta::identifier_of(std::meta::attributes_of(^^Foo)[1])) :]++;
    return s.nodiscard == 1 && s.maybe_unused == 1;
}

consteval bool testSplicingAttr() {
  constexpr auto r = ^^[[deprecated]];
  struct [[ [: r :] ]] {} iAmDeprecated = {};
  return std::meta::identifier_of(std::meta::attributes_of(^^iAmDeprecated)[0]) == "deprecated";
}

consteval bool testSplicingType() {
  constexpr auto r = ^^Foo;
  struct [[ [: r :] ]] Bar;
  return std::meta::identifier_of(std::meta::attributes_of(^^Bar)[0]) == "nodiscard"
    &&  std::meta::identifier_of(std::meta::attributes_of(^^Bar)[1]) == "maybe_unused";
}

int main(int, char**) {

  static_assert(testIsAttribute(), "Fail ^^[[ ]]");

  static_assert(testIdentifierOf(), "Fail std::meta::identifiers_of");

  static_assert(testAttributesOfAttr(), "Fail std::meta::attributes_of(^^[[ ]])");
  static_assert(testAttributesOfType(), "Fail std::meta::attributes_of(^^MyType)");

  static_assert(testSplicingAttr(), "Fail [[ [: ^^attr :] ]]");
  static_assert(testSplicingType(), "Fail [[ [: ^^type :] ]]");

  return 0;
}

zebullon added 4 commits October 6, 2024 14:51
@zebullax zebullax changed the title P3385 [WIP] P3385 Oct 13, 2024
@@ -2991,6 +2991,7 @@ DEF_TRAVERSE_STMT(CXXReflectExpr, {
case ReflectionKind::Namespace:
case ReflectionKind::BaseSpecifier:
case ReflectionKind::DataMemberSpec:
case ReflectionKind::Attribute: // TODO P3385 ?
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I need this at all.

zebullon added 2 commits October 27, 2024 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant